<%@tag import="java.sql.Connection"%>
<%@tag import="by.epam.communalservices.dao.jdbc.JdbcDaoJkhTeam"%>
<%@tag import="by.epam.communalservices.entity.JkhTeam"%>
<%@ attribute name="id" required="true"%>
<%@ attribute name="name" required="true"%>
<%@ attribute name="surname" required="true"%>
<%@ attribute name="patronymic" required="true"%>
<%@ attribute name="owner" required="true"%>
<%@ attribute name="submit" required="true"%>
<%@ attribute name="delete" required="true"%>
<%

    request.setAttribute("message", "");

    Connection con = (Connection) request.getSession().getAttribute("currentConnection");
    JkhTeam jkhTeam = null;
    JdbcDaoJkhTeam daoTeam = new JdbcDaoJkhTeam(con);

    try {
        if (getJspContext().getAttribute("id") != "" && Long.parseLong(getJspContext().getAttribute("id").toString()) > 0) {
            jkhTeam = daoTeam.read(Long.parseLong(getJspContext().getAttribute("id").toString()));
            if (getJspContext().getAttribute("submit") != "") {
                jkhTeam.setName(getJspContext().getAttribute("name").toString());
                jkhTeam.setSurname(getJspContext().getAttribute("surname").toString());
                jkhTeam.setPatronymic(getJspContext().getAttribute("patronymic").toString());
                jkhTeam.setIdOwner(Long.parseLong(getJspContext().getAttribute("owner").toString()) );
                daoTeam.update(jkhTeam.getId(), jkhTeam);
                request.setAttribute("message", "Team updated");
            }
        } else if (getJspContext().getAttribute("delete") != ""
                && Long.parseLong(getJspContext().getAttribute("delete").toString()) > 0) {
            daoTeam.delete(Long.parseLong(getJspContext().getAttribute("delete").toString()));
            request.setAttribute("message", "Team deleted");
        } else {
            jkhTeam = new JkhTeam();
            if (getJspContext().getAttribute("submit") != "") {
                jkhTeam.setName(getJspContext().getAttribute("name").toString());
                jkhTeam.setSurname(getJspContext().getAttribute("surname").toString());
                jkhTeam.setPatronymic(getJspContext().getAttribute("patronymic").toString());
                jkhTeam.setIdOwner(Long.parseLong(getJspContext().getAttribute("owner").toString()));
                jkhTeam.setId(daoTeam.create(jkhTeam));
                request.setAttribute("message", "Team Created");
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
        out.println(e.getMessage() + "<br>");
        for (StackTraceElement el : e.getStackTrace()) {
            out.println(el.toString() + "<br>");
        }
    }
    if (jkhTeam == null) {
        request.setAttribute("form", false);
        if (getJspContext().getAttribute("delete") == "") {
            request.setAttribute("message", "Team Not Found");
        }
    } else {
        request.setAttribute("form", true);
        request.setAttribute("team", jkhTeam);
    }
    
%>